﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace LS.FrmUI.Dal
{
    public class DBHelper
    {
        public static string connectionString = @"Data Source=" + System.Environment.CurrentDirectory +"\\ls.db"+ ";Version=3;";

        //不带参数的执行命令
        public static int ExecuteNonQuery(string safeSql)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                SQLiteCommand cmd = new SQLiteCommand(safeSql, connection);
                return cmd.ExecuteNonQuery();
            }
        }
        //带参数的执行命令
        public static int ExecuteNonQuery(string sql, params SQLiteParameter[] values)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                SQLiteCommand cmd = new SQLiteCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }


        public static object ExecuteScalar(string sql, params SQLiteParameter[] values)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                SQLiteCommand cmd = new SQLiteCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteScalar();
            }
        }

        public static SQLiteDataReader GetReader(string safeSql)
        {
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            connection.Open();
            SQLiteCommand cmd = new SQLiteCommand(safeSql, connection);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public static SQLiteDataReader GetReader(string sql, params SQLiteParameter[] values)
        {
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            connection.Open();
            SQLiteCommand cmd = new SQLiteCommand(sql, connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public static DataTable GetTable(string safeSql)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                DataSet ds = new DataSet();
                SQLiteCommand cmd = new SQLiteCommand(safeSql, connection);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }

        public static DataTable GetTable(string sql, params SQLiteParameter[] values)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                DataSet ds = new DataSet();
                SQLiteCommand cmd = new SQLiteCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}
